const tag ="popup.js";
// 保存数据到 Chrome Storage
function saveDataToStorage(data) {
  chrome.storage.sync.set({ 'myData': data }, function () {
    console.log('Data saved to Chrome Storage');
  });
}

// 从 Chrome Storage 中检索数据
function getDataFromStorage(callback) {
  chrome.storage.sync.get('myData', function (result) {
    var data = result.myData || 'Default Value';
    callback(data);
  });
}
//test
/*
document.addEventListener('DOMContentLoaded', function () {

  var messageElement = document.getElementById('message');
  messageElement.textContent = 'Hello, this is a simple Chrome extension!';

  
  var inputElement = document.getElementById('input1');
  var saveButton = document.getElementById('saveButton');
  var loadButton = document.getElementById('loadButton');

  inputElement.addEventListener('input', function () {
    messageElement.textContent = inputElement.value;
  });



  // 保存数据到 Chrome Storage
  saveButton.addEventListener('click', function() {
    var inputValue = inputElement.value;
    getDataFromStorage(inputValue,function(){
      console.log('Data saved to Chrome Storage');
    });
  });

  // 从 Chrome Storage 中读取数据
  loadButton.addEventListener('click', function() {
    chrome.storage.sync.get('myData', function(result) {
      var data = result.myData || 'No data found';
      messageElement.textContent = data;
    });
  });
});
*/

document.addEventListener('DOMContentLoaded', function () {
  console.log(tag,"loaded");
  var input = document.getElementById('input2');
  var btnSend = document.getElementById('btnSend');

  input.value = "test";

  btnSend.addEventListener('click', function () {
    // 发送消息到 Content Script
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, {message: "你好 Content Script,我是popup.js"});
    });

    // 发送消息到 Background 页面
    chrome.runtime.sendMessage({ message: "你好 Background,我是popup.js" }, function (response) {
      console.log(tag,"[接收] " + response.message);
      var result = document.getElementById('input3');
      result.value =result.value +"\n"+ tag+ "[接收] " + response.message;
    });

  });
});

// 添加消息监听器
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  if (message && message.message) {
    var result = document.getElementById('input3');
    result.value =result.value +"\n"+ tag+ "[接收2] " + message.message;
  }
});